using Microsoft.AspNetCore.Mvc;
using TMS.Application.Common.Interface;
using TMS.Application.RequestDto;
using TMS.Application.Utils;
using TMS.Domain.Entity;

namespace TMS.Api.Controllers
{
    [ApiController]
    public class GetTeaSubController : ControllerBase
    {
        private readonly IRepository<Subject> _Subject;
        private readonly IRepository<Teachers> _Teachers;
        public GetTeaSubController(IRepository<Teachers> Teachers, IRepository<Subject> Subject)
        {
            _Teachers = Teachers;

            _Subject = Subject;
        }

        // 教师添加科目
        [HttpPost("/GetAddTeaSub")]
        public async Task<object> AddTeaSub(AddTeaSubDto addTeaSubDto)
        {
            var teaSub = new Subject
            {

                SubjectName = addTeaSubDto.SubName,

                TeacherId = addTeaSubDto.TeaId

            };

            try
            {
                await _Subject.AddAsync(teaSub);

                return new
                {
                    code = 200,
                    msg = "添加成功",
                    data = ""
                };
            }
            catch
            {
                return new
                {
                    code = 400,
                    msg = "添加失败",
                    data = ""
                };
            }



        }

        // 删除科目
        [HttpDelete("/GetDelSub/{Id}")]
        public async Task<object> DelSub(Guid Id)
        {
            try
            {

                await _Subject.DeleteAsync(Id, true);

                return new
                {
                    code = 200,
                    msg = "已删除",
                    data = Sub()
                };
            }
            catch (Exception ex)
            {
                return new
                {
                    code = 400,
                    msg = "删除失败！",
                    data = ex
                };
            }



        }


        // 编辑科目
        [HttpPost("/GetUpdateSub")]
        public async Task<object> UpdateSub(UpdateSubDto updateSubDto)
        {

            var subject = _Subject.Table.FirstOrDefault(x => x.Id == updateSubDto.Id);

            if (subject != null)
            {

                subject.SubjectName = updateSubDto.SubjectName;

                try
                {
                    await _Subject.UpdateAsync(subject);

                    return new
                    {
                        code = 200,
                        msg = "已更新",
                        data = Sub()
                    };

                }
                catch (Exception ex)
                {
                    return new
                    {
                        code = 400,
                        msg = "更新失败！",
                        data = ex
                    };

                }


            }
            else
            {

                return new
                {
                    code = 400,
                    msg = "更新失败！错误的科目信息",
                    data = ""
                };

            }



        }


        // 获得科目
        [HttpGet("/GetSub")]
        public object Sub()
        {

            var subjects = _Subject.Table.ToList();

            var teachers = _Teachers.Table.ToList();

            List<object> list = new List<object>();

            foreach (var item in subjects)
            {
                var teacher = teachers.FirstOrDefault(x => x.TeacherId == item.TeacherId);

                var teacherName = teacher == null ? "未知教师" : teacher.TeacherName;

                var data = new
                {

                    Id = item.Id,

                    SubjectName = item.SubjectName,

                    teacherName

                };

                list.Add(data);
            }

            return list;
        }

        // 获得某个教师的科目
        [HttpGet("/GetTeaSub/{id}")]
        public object TeaSub(string id)
        {
            var sub = _Subject.Table.ToList();

            var subjects = _Subject.Table.Where(x => x.TeacherId == id).ToList();

            List<object> list = new List<object>();

            foreach (var item in subjects)
            {

                var data = new
                {

                    value = item.Id,

                    TeaGuid = item.TeacherId,

                    label = item.SubjectName

                };

                list.Add(data);
            }


            return list;

        }


    }
}